package com.dys.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @className: com.dys.controller.RequestParamController
 * @description: com.dys.controller
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/10/26 15:09
 */
@Controller
public class RequestParamController {

    /**
     * 紧耦合方式参数注入-使用传统的HttpServletRequest对象获取参数 javax.servlet
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getDataByRequest.do", method = RequestMethod.GET)
    public String getDataByRequestParam(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:" + username + " password:" + password);
        return "requestSuccess";
    }

    /**
     * 解耦合方式参数注入：HttpServletRequest对象获取参数，通过springmvc框架功能，自动转换参数
     * 处理单元中的参数列表中的参数名必须和请求中的参数名称一致，如果不一致，则需要使用@RequestParam()转换
     * 如果数据类型不一致，则需要进行数据类型的转换
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/getDataByNoRequest.do", method = RequestMethod.GET)
    public String getDataByNoRequestParam(String username, @RequestParam("pwd") String password) {
        System.out.println("no request username:" + username + " password:" + password);
        return "requestSuccess";
    }
}
